﻿using netDxf;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Tables;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestDxfDocument
{
    class YCJ
    {
        public static void RhombicInsert2(Dictionary<string, Object> data)    //修光刃正方形刀片 图2
        { 
            DxfDocument dxf = new DxfDocument();
            



            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer center = new Layer("center") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            /*
            double tic = Convert.ToDouble(data["ic"]);
            double scale = Convert.ToDouble(data["scale"]); //放大系数
            if (scale == 0 && tic < 9)
            {
                scale = 3.0;
            }
            else if (scale == 0 && tic < 20)
            {
                scale = 2.5;
            }
            else if (scale == 0)
            {
                scale = 2.0;
            }
            */

            string path = Convert.ToString(data["path"]);
            double pi = Math.PI;

            double IC = 12.7;
            double S = 4.76;
            double L = IC;
            double M = 0.9;
            double BS = 1.4;
            double EPSR = 90;
            double KRINS = 75;
            double KCH = 37;
            double AN = 15;
            double BCH = 1;
            double AS = 11;

            //double EPSRN = 75 * (Math.PI / 180);

            double AS_1 = AS * (Math.PI / 180);

            double EPSR_1 = EPSR * (Math.PI / 180);
            double KRINS_1 = KRINS * (Math.PI / 180);

            double An_1 = AN * (Math.PI / 180);
            double KCH_1 = KCH * (Math.PI / 180);

            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X - (BS * Math.Cos(EPSR_1 - KRINS_1)), v1.Y + (BS * Math.Sin(EPSR_1 - KRINS_1)));
            Vector2 v3 = new Vector2(v2.X - (BCH * Math.Sin(KCH_1)), v2.Y + BCH * Math.Cos(KCH_1));

            Vector2[] Line1 = { v1, v2 };
            fun.drawBLine(dxf, Line1, 0.3, contour);

            Vector2[] Line2 = { v3, v2 };
            fun.drawBLine(dxf, Line2, 0.3, contour);

            Vector2 v14 = new Vector2(v3.X, v1.Y + L / 2);
            Vector2 v15 = new Vector2(v14.X + L, v14.Y);
            Vector2 v16 = new Vector2(v14.X + L / 2, v1.Y + L);
            Vector2 v17 = new Vector2(v16.X, v16.Y - L);
            Vector2 v13 = new Vector2(v14.X + L / 2, v14.Y);
            Vector2 v18 = new Vector2(v3.X, v1.Y + L);
            Vector2 v19 = new Vector2(v3.X + L, v1.Y);


                Line line3 = fun.mirrorline(new Line(v1, v2), v14, v15);
                Vector2[] lsline1 = { new Vector2(line3.StartPoint.X, line3.StartPoint.Y), new Vector2(line3.EndPoint.X, line3.EndPoint.Y) };

                Line line4 = fun.mirrorline(new Line(v2, v3), v14, v15);
                Vector2[] lsline2 = { new Vector2(line4.StartPoint.X, line4.StartPoint.Y), new Vector2(line4.EndPoint.X, line4.EndPoint.Y) };



                Line line5 = fun.mirrorline(new Line(v1, v2), v18, v19);
                Vector2[] lsline4 = { new Vector2(line5.StartPoint.X, line5.StartPoint.Y), new Vector2(line5.EndPoint.X, line5.EndPoint.Y) };

                Line line6 = fun.mirrorline(new Line(v3, v2), v18, v19);
                Vector2[] lsline5 = { new Vector2(line6.StartPoint.X, line6.StartPoint.Y), new Vector2(line6.EndPoint.X, line6.EndPoint.Y) };


                Line line7 = fun.mirrorline(new Line(v1, v2), v16, v17);
                Vector2[] lsline7 = { new Vector2(line7.StartPoint.X, line7.StartPoint.Y), new Vector2(line7.EndPoint.X, line7.EndPoint.Y) };

            Line line8 = fun.mirrorline(new Line(v3, v2), v16, v17);
            Vector2[] lsline8 = { new Vector2(line8.StartPoint.X, line8.StartPoint.Y), new Vector2(line8.EndPoint.X, line8.EndPoint.Y) };


            Vector2 v4 = new Vector2(line4.EndPoint.X, line4.EndPoint.Y);
            Vector2 v5 = new Vector2(line3.EndPoint.X, line3.EndPoint.Y);
            Vector2 v6 = new Vector2(line3.StartPoint.X, line3.StartPoint.Y);
            Vector2 v7 = new Vector2(line6.StartPoint.X, line6.StartPoint.Y);
            Vector2 v8 = new Vector2(line6.EndPoint.X, line6.EndPoint.Y);
            Vector2 v9 = new Vector2(line5.StartPoint.X, line5.StartPoint.Y);
            Vector2 v10 = new Vector2(line8.StartPoint.X, line8.StartPoint.Y);
            Vector2 v11 = new Vector2(line7.EndPoint.X, line7.EndPoint.Y);
            Vector2 v12 = new Vector2(line7.StartPoint.X, line7.StartPoint.Y);

            Vector2[] Line9 = { v3, v4 };
            fun.drawBLine(dxf, Line9, 0.3, contour);

            Vector2[] Line10 = { v12, v1 };
            fun.drawBLine(dxf, Line10, 0.3, contour);
            Vector2[] Line11 = { v6, v7 };
            fun.drawBLine(dxf, Line11, 0.3, contour);
            Vector2[] Line12 = { v9, v10 };
            fun.drawBLine(dxf, Line12, 0.3, contour);

            Circle circle00 = new Circle(v13, IC / 2);
            circle00.Layer = center;
            circle00.Lineweight = Lineweight.W50;
            circle00.Linetype = Linetype.DashDot;
            dxf.AddEntity(circle00);


            Vector2 v20 = new Vector2(v15.X + IC, v15.Y);
            Vector2 v21 = new Vector2(v20.X - S / 2 * Math.Sin(An_1), v20.Y + S / 2 * Math.Cos(An_1));
            Vector2 v23 = new Vector2(v20.X - S, v20.Y);
            Vector2 v22 = new Vector2(v23.X - S / 2 * Math.Cos(EPSR_1 - An_1), v23.Y + S / 2 * Math.Sin(EPSR_1 - An_1));
            Vector2 v24 = new Vector2(v12.X + 2 * L, v12.Y);
            Vector2 v25 = new Vector2(v24.X, v24.Y + L);
            Vector2 v26 = new Vector2(v25.X - S, v25.Y - S * Math.Tan(AS_1));
            Vector2 v27 = new Vector2(v24.X - S, v24.Y + S * Math.Tan(AS_1));

            Vector2 v28 = new Vector2(v24.X - 3, v24.Y);

            Vector2 v29 = new Vector2(v15.X + 5, v15.Y);
            Vector2 v30 = new Vector2(v12.X + 1.31, v12.Y + 0.31);
            Vector2 v31 = new Vector2(v15.X + 12.51, v15.Y - 8.37);

            List<Vector3> points00 = new List<Vector3>
           {
                 new Vector3(v22.X,v22.Y , 0),
               new Vector3(v22.X+1,v22.Y+0.5 , 0),
               new Vector3(v22.X+2,v22.Y , 0),


                  new Vector3(v21.X ,v21.Y, 0),


            };

            Spline spline00 = new Spline(points00);
            spline00.Layer = contour;
            DxfDocument doc00 = new DxfDocument();
            dxf.AddEntity(spline00);

            Vector2[] Line13 = { v23, v20 };
            fun.drawBLine(dxf, Line13, 0.3, contour);
            Vector2[] Line14 = { v23, v22 };
            fun.drawBLine(dxf, Line14, 0.3, contour);
            Vector2[] Line15 = { v20, v21 };
            fun.drawBLine(dxf, Line15, 0.3, contour);

            LwPolyline poly1 = new LwPolyline();
            poly1.Vertexes.Add(new LwPolylineVertex(v24));
            poly1.Vertexes.Add(new LwPolylineVertex(v25));
            poly1.Vertexes.Add(new LwPolylineVertex(v26));
            poly1.Vertexes.Add(new LwPolylineVertex(v27));
            poly1.Vertexes.Add(new LwPolylineVertex(v24));

            //            poly1.SetConstantWidth(0.05);
            poly1.IsClosed = true;
            fun.PolyFill(dxf, poly1, 0.4, contour);

            String[] htype1 = new String[100];
            Line[] hline1 = new Line[100];
            Arc[] harc1 = new Arc[100];
            int hnumst1 = poly1.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumst1; i++)
            {
                htype1[i] = poly1.Explode()[i].Type.ToString();    //将所有元素的类型记录成一个数组
                if (htype1[i] == "Line")
                {
                    hline1[i] = (Line)poly1.Explode()[i];
                }
                if (htype1[i] == "Arc")
                {
                    harc1[i] = (Arc)poly1.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype1, hnumst1, 45, 5, hline1, harc1, hatching);



            DimensionStyle dimensionStyle = fun.CreateDimStyle();
            dimensionStyle.ArrowSize = 0.2;
            dimensionStyle.DimArrow1 = DimensionArrowhead.Closedfilled;
            dimensionStyle.DimArrow2 = DimensionArrowhead.Closedfilled;
            dimensionStyle.TextHeight = 0.3;
            double offset = 3;

            if (AN == 11)
            {

                Line Line3 = fun.mirrorline(new Line(v1, v2), v14, v15);
                Vector2[] lsl1 = { new Vector2(Line3.StartPoint.X, Line3.StartPoint.Y), new Vector2(Line3.EndPoint.X, Line3.EndPoint.Y) };
                fun.drawBLine(dxf, lsl1, 0.3, contour);
                Line Line4 = fun.mirrorline(new Line(v2, v3), v14, v15);
                Vector2[] lsl2 = { new Vector2(Line4.StartPoint.X, Line4.StartPoint.Y), new Vector2(Line4.EndPoint.X, Line4.EndPoint.Y) };
                fun.drawBLine(dxf, lsl2, 0.3, contour);


                Line Line5 = fun.mirrorline(new Line(v1, v2), v18, v19);
                Vector2[] lsLine4 = { new Vector2(Line5.StartPoint.X, Line5.StartPoint.Y), new Vector2(Line5.EndPoint.X, Line5.EndPoint.Y) };
                fun.drawBLine(dxf, lsline4, 0.3, contour);
                Line Line6 = fun.mirrorline(new Line(v3, v2), v18, v19);
                Vector2[] lsLine5 = { new Vector2(Line6.StartPoint.X, Line6.StartPoint.Y), new Vector2(Line6.EndPoint.X, Line6.EndPoint.Y) };
                fun.drawBLine(dxf, lsLine5, 0.3, contour);

                Line Line7 = fun.mirrorline(new Line(v1, v2), v16, v17);
                Vector2[] lsl7 = { new Vector2(Line7.StartPoint.X, Line7.StartPoint.Y), new Vector2(Line7.EndPoint.X, Line7.EndPoint.Y) };
                fun.drawBLine(dxf, lsl7, 0.3, contour);
                Line Line8 = fun.mirrorline(new Line(v3, v2), v16, v17);
                Vector2[] lsLine8 = { new Vector2(Line8.StartPoint.X, Line8.StartPoint.Y), new Vector2(Line8.EndPoint.X, Line8.EndPoint.Y) };
                fun.drawBLine(dxf, lsLine8, 0.3, contour);
                //标注
                LinearDimension dim8 = new LinearDimension(new Line(v10, v11), offset, 235, dimensionStyle);
                dim8.UserText = "≈1";
                dxf.AddEntity(dim8);
                Angular2LineDimension angdim2 = new Angular2LineDimension(new Line(v10, v15), new Line(v10, v29), offset, dimensionStyle);
                angdim2.UserText = "37°30′";
                dxf.AddEntity(angdim2);
                Angular2LineDimension angdim6 = new Angular2LineDimension(new Line(v20, v23), (new Line(v20, v11)), offset + 3, dimensionStyle);
                angdim6.UserText = "15°±1°";
                dxf.AddEntity(angdim6);

                Angular2LineDimension angdim7 = new Angular2LineDimension(new Line(v24, v27), (new Line(v24, v28)), offset + 5, dimensionStyle);
                angdim7.UserText = "11°±1°";
                dxf.AddEntity(angdim7);


            }
            else
            {
                Angular2LineDimension angdim8 = new Angular2LineDimension(new Line(v20, v23), (new Line(v20, v11)), offset + 3, dimensionStyle);
                angdim8.UserText = "20°±30°";
                dxf.AddEntity(angdim8);

                Angular2LineDimension angdim9 = new Angular2LineDimension(new Line(v24, v27), (new Line(v24, v28)), offset + 5, dimensionStyle);
                angdim9.UserText = "20°±30°";
                dxf.AddEntity(angdim9);

            }

            //文本样式

            //线段尺寸标注
            //尺寸1
            LinearDimension dim1 = new LinearDimension(new Line(v4, v9), offset, 360, dimensionStyle);
            dim1.UserText = "l";
            dxf.AddEntity(dim1);

            LinearDimension dim2 = new LinearDimension(new Line(v1, v6), offset, 90, dimensionStyle);
            dim2.UserText = "l";
            dxf.AddEntity(dim2);
            LinearDimension dim3 = new LinearDimension(new Line(v14, v15), offset - 3, 360, dimensionStyle);
            dim3.UserText = "d";
            dxf.AddEntity(dim3);
            LinearDimension dim4 = new LinearDimension(new Line(v17, v12), offset, 120, dimensionStyle);
            dim4.UserText = "m";
            dxf.AddEntity(dim4);
            // LinearDimension dim5 = new LinearDimension(new Line(v21, v16), offset, 110, dimensionStyle);
            //dim5.UserText = "m";
            //dxf.AddEntity(dim5);
            LinearDimension dim6 = new LinearDimension(new Line(v25, v26), offset + 2, 360, dimensionStyle);
            dim6.UserText = "S";
            dxf.AddEntity(dim6);
            LinearDimension dim7 = new LinearDimension(new Line(v11, v12), offset, 197, dimensionStyle);
            dim7.UserText = "bs";
            dxf.AddEntity(dim7);




            Angular2LineDimension angdim1 = new Angular2LineDimension(new Line(v10, v15), new Line(v12, v17), offset + 3, dimensionStyle);
            angdim1.UserText = "εr";
            dxf.AddEntity(angdim1);


            Angular2LineDimension angdim3 = new Angular2LineDimension((new Line(v30, v9)), new Line(v30, v31), offset, dimensionStyle);
            angdim3.UserText = "xr＝75°";
            dxf.AddEntity(angdim3);


            Angular2LineDimension angdim5 = new Angular2LineDimension(new Line(v3, v4), (new Line(v12, v11)), offset + 19, dimensionStyle);
            angdim5.UserText = "φ";
            dxf.AddEntity(angdim5);





            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);


           


               }
        }
}
